package cn.gok.mmall.common;



import com.fasterxml.jackson.annotation.JsonInclude;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import java.io.Serializable;

/**
 * @Author: chenfan
 * @Description: 响应结果集类
 *
 *
 *    @JsonInclude(JsonInclude.Include.NON_NULL)含义： 当属性值为null的，该属性被忽略，不进行序列化
 */

@JsonInclude(JsonInclude.Include.NON_NULL)
public class ServerResponse<T> implements Serializable {

    private int status;
    private String msg;
    private T data;

    public ServerResponse() {
    }

    public ServerResponse(int status) {
        this.status = status;
    }

    public ServerResponse(int status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    public ServerResponse(int status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }


    /**
     *   成功时直接调用
     */
    public static <T> ServerResponse<T> success(){
        return new ServerResponse<>(ResponseCode.SUCCESS.getCode());
    }
    /**
     *   成功时，有消息提示时直接调用
     */
    public static <T> ServerResponse<T> success(String msg){
        return new ServerResponse<>(ResponseCode.SUCCESS.getCode(),msg);
    }

    /**
     *   成功时，有返回数据时直接调用
     */
    public static <T> ServerResponse<T> success(T data){
        return new ServerResponse<>(ResponseCode.SUCCESS.getCode(),null,data);
    }


    /**
     *   失败时直接调用
     */
    public static <T> ServerResponse<T> error(){
        return new ServerResponse<>(ResponseCode.ERROR.getCode(),ResponseCode.ERROR.getDesc());
    }

    /**
     *   失败时，有消息提示时直接调用
     */
    public static <T> ServerResponse<T> error(String msg){
        return new ServerResponse<>(ResponseCode.ERROR.getCode(),msg);
    }


    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
